package com.bitbakery.plugin.cl.lang;

import com.bitbakery.plugin.cl.CLConstants;
import com.bitbakery.plugin.cl.CLIcons;
import com.bitbakery.plugin.cl.config.CLConfigurationForm;
import com.bitbakery.plugin.cl.config.CLImplementationType;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.util.DefaultJDOMExternalizer;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import org.jdom.Element;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class CLReplApplicationComponent implements ApplicationComponent, Configurable, JDOMExternalizable {
    private CLConfigurationForm form;

    // Common Lisp settings - these guys are persisted via JDOMExternalizable
    public CLImplementationType type;
    public String homeDirectory;
    public String initializationFile;
    public String lispExecutable;


    public void initComponent() {
        // Do nothing
    }

    public void disposeComponent() {
        // Do nothing
    }

    public String getComponentName() {
        return "CLReplApplicationComponent";
    }

    public String getHomeDirectory() {
        return homeDirectory;
    }

    public void setHomeDirectory(final String homeDirectory) {
        this.homeDirectory = homeDirectory;
    }

    public String getInitializationFile() {
        return initializationFile;
    }

    public void setInitializationFile(final String initializationFile) {
        this.initializationFile = initializationFile;
    }

    public String getLispExecutable() {
        return lispExecutable;
    }

    public void setLispExecutable(final String lispExecutable) {
        this.lispExecutable = lispExecutable;
    }

    @Nls
    public String getDisplayName() {
        return CLConstants.COMMON_LISP;
    }

    public Icon getIcon() {
        return CLIcons.COMMON_LISP_ICON;
    }

    @Nullable
    @NonNls
    public String getHelpTopic() {
        return null;
    }

    public JComponent createComponent() {
        if (form == null) {
            form = new CLConfigurationForm();
        }
        return form.getRootComponent();
    }

    public boolean isModified() {
        return form != null && form.isModified(this);
    }

    public void apply() throws ConfigurationException {
        if (form != null) {
            form.getData(this);
        }
    }

    public void reset() {
        if (form != null) {
            form.setData(this);
        }
    }

    public void disposeUIResources() {
        form = null;
    }

    public void readExternal(Element element) throws InvalidDataException {
        DefaultJDOMExternalizer.readExternal(this, element);
    }

    public void writeExternal(Element element) throws WriteExternalException {
        DefaultJDOMExternalizer.writeExternal(this, element);
    }
}
